import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

class Solution {

    public int findNumberOfLIS(int[] nums) {
        int n = nums.length;
        int[] len = new int[n];
        int[] count = new int[n];
        for(int i = 0; i < n; i++){
            len[i] = count[i] = 1;
        }

        int retcount = 1, retlen = 1;
        for(int i = 1; i < n; i++){
            for(int j = 0; j < i; j++){
                if(nums[i] > nums[j]){
                    if(len[j] + 1 == len[i]){
                        count[i] += count[j];
                    }else if(len[j] + 1 > len[i]){
                        len[i] = len[j] + 1;
                        count[i] = count[j];
                    }
                }
            }
            if(retlen == len[i]){
                retcount += count[i];
            }else if(retlen < len[i]){
                retlen = len[i];
                retcount = count[i];
            }
        }
        return retcount;
    }

    public int findLongestChain(int[][] pairs) {
        Arrays.sort(pairs, (a, b) -> a[0] - b[0]);  //预处理
        int n = pairs.length;
        int[] dp = new int[n];
        for(int i = 0; i < n; i++){
            dp[i] = 1;
        }

        int ret = 1;
        for(int i = 1; i < n; i++){
            for(int j = 0; j < n; j++){
                if(pairs[j][1] < pairs[i][0]){
                    dp[i] = Math.max(dp[j] + 1, dp[i]);
                }
            }
            ret = Math.max(ret, dp[i]);
        }
        return ret;
    }

    public int longestSubsequence(int[] arr, int difference) {
        Map<Integer, Integer> hash = new HashMap<>();

        int ret = 1;
        for(int a : arr){
            hash.put(a, hash.getOrDefault(a - difference, 0) + 1);
            ret = Math.max(ret, hash.get(a));
        }
        return ret;
    }
}